package com.jzx.provider.ucenter.consul.controller;

import com.jzx.provider.common.constants.HttpHeader;
import com.jzx.provider.common.result.InvokeResult;
import com.jzx.provider.ucenter.UcenterService;
import com.jzx.provider.ucenter.client.CurrentUser;
import com.jzx.provider.ucenter.client.CurrentUserInfo;
import com.jzx.provider.ucenter.consul.dto.command.*;
import com.jzx.provider.ucenter.consul.dto.result.ConsulResult;
import com.jzx.provider.ucenter.consul.service.ConsulService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.Pattern;

@RestController
@RequestMapping("/consul")
public class ConsulController {
    @Autowired
    private ConsulService consulService;

    @Autowired
    private UcenterService ucenterService;

    @GetMapping("/sendCode/{mobile}")
    public InvokeResult<Void> sendCode(@PathVariable @Pattern(regexp = "^1\\d{10}$") String mobile) {
        consulService.sendCode(mobile);
        return InvokeResult.success();
    }

    @GetMapping("/logout")
    public InvokeResult<Void> logout(@RequestHeader(HttpHeader.TOKEN) String token) {
        ucenterService.logout(token);
        return InvokeResult.success();
    }

    @PostMapping("/login")
    public InvokeResult<ConsulResult> login(@RequestBody @Validated ConsulLoginCommand consulLoginCommand) {
        return InvokeResult.success(consulService.login(consulLoginCommand));
    }

    @PostMapping("/register")
    public InvokeResult<Void> register(@RequestBody @Validated ConsulRegisterCommand consulRegisterCommand) {
        consulService.register(consulRegisterCommand);
        return InvokeResult.success();
    }

    @PostMapping("changePwdByPwd")
    public InvokeResult<Void> changePwdByPwd(@CurrentUserInfo CurrentUser currentUser,@RequestBody ConsulChangePwdByPwdCommand consulChangePwdByPwdCommand){
        consulService.changePwdByPwd(currentUser.getId(),consulChangePwdByPwdCommand);
        return InvokeResult.success();
    }

    @GetMapping("/findConsul")
    public InvokeResult<ConsulResult> findConsul(@CurrentUserInfo CurrentUser currentUser) {
        return InvokeResult.success(consulService.findById(currentUser.getId()));
    }

    @PostMapping("/checkIdCard")
    public InvokeResult<Void> checkIdCard(@CurrentUserInfo CurrentUser currentUser, @RequestBody @Validated ConsulIdCheckCommand consulIdCheckCommand) {
        consulService.checkIdCard(currentUser.getId(), consulIdCheckCommand);
        return InvokeResult.success();
    }

    @PostMapping("/uploadHead")
    public InvokeResult<String> uploadHead(@CurrentUserInfo CurrentUser currentUser, @RequestBody ConsulHeadPicCommand consulHeadPicCommand) {
        consulService.uploadHead(currentUser.getId(), consulHeadPicCommand.getHeadPic());
        return InvokeResult.success();
    }
}
